package org.edu.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.edu.bean.Role_user;
import org.edu.service.UserService;

import com.opensymphony.xwork2.ActionSupport;

public class Role_userController extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Resource
	UserService userService ;
	
	private int roleId ;
	private int userId;
	private String result;
	@SuppressWarnings("unchecked")
	public String create(){
		Role_user roleUser = new Role_user() ;
		roleUser.setRoleId(roleId);
		roleUser.setUserId(userId);
		
		userService.userAddRole(roleUser);
		Map map = new HashMap();  
        map.put("status", true);
		JSONObject object = JSONObject.fromObject(map);
		result = object.toString();
		return SUCCESS ;
	}
	
	@SuppressWarnings("unchecked")
	public String del(){
		List<Role_user> roleUsers = userService.selectRoleUsers(userId, roleId);
		if(roleUsers.size()>0){
			userService.delRole_user(roleUsers.get(0));
			Map map = new HashMap();
	        map.put("status", true);
			JSONObject object = JSONObject.fromObject(map);
			result = object.toString();
			return SUCCESS ;
		}
		return ERROR ;
	}
	
	
	public int getRoleId() {
		return roleId;
	}
	public void setRoleId(int roleId) {
		this.roleId = roleId;
	}
	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
	
}
